package com.castle.fortress.admin.oa.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.DateHolidayEntity;
import com.castle.fortress.admin.oa.dto.DateHolidayDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.oa.service.DateHolidayService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 日期节假日表 api 控制器
 *
 * @author whc
 * @since 2022-11-04
 */
@Api(tags="日期节假日表api管理控制器")
@RestController
@RequestMapping("/api/oa/dateHoliday")
public class ApiDateHolidayController {
    @Autowired
    private DateHolidayService dateHolidayService;


    /**
     * 日期节假日表的分页展示
     * @param dateHolidayDto 日期节假日表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("日期节假日表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<DateHolidayDto>> pageDateHoliday(DateHolidayDto dateHolidayDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<DateHolidayDto> page = new Page(pageIndex, pageSize);

        IPage<DateHolidayDto> pages = dateHolidayService.pageDateHoliday(page, dateHolidayDto);
        return RespBody.data(pages);
    }

    /**
     * 日期节假日表保存
     * @param dateHolidayDto 日期节假日表实体类
     * @return
     */
    @ApiOperation("日期节假日表保存")
    @PostMapping("/save")
    public RespBody<String> saveDateHoliday(@RequestBody DateHolidayDto dateHolidayDto){
        if(dateHolidayDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            DateHolidayEntity dateHolidayEntity = ConvertUtil.transformObj(dateHolidayDto,DateHolidayEntity.class);
        if(dateHolidayService.save(dateHolidayEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日期节假日表编辑
     * @param dateHolidayDto 日期节假日表实体类
     * @return
     */
    @ApiOperation("日期节假日表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateDateHoliday(@RequestBody DateHolidayDto dateHolidayDto){
        if(dateHolidayDto == null || dateHolidayDto.getId() == null || dateHolidayDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            DateHolidayEntity dateHolidayEntity = ConvertUtil.transformObj(dateHolidayDto,DateHolidayEntity.class);
        if(dateHolidayService.updateById(dateHolidayEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日期节假日表删除
     * @param ids 日期节假日表id集合
     * @return
     */
    @ApiOperation("日期节假日表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteDateHoliday(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(dateHolidayService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日期节假日表详情
     * @param id 日期节假日表id
     * @return
     */
    @ApiOperation("日期节假日表详情")
    @GetMapping("/info")
    public RespBody<DateHolidayDto> infoDateHoliday(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            DateHolidayEntity dateHolidayEntity = dateHolidayService.getById(id);
            DateHolidayDto dateHolidayDto = ConvertUtil.transformObj(dateHolidayEntity,DateHolidayDto.class);
        return RespBody.data(dateHolidayDto);
    }


}
